<script setup lang="ts">
import {Delete, Document, Edit, Plus} from '@element-plus/icons-vue'
import {ref, watch} from 'vue'

const props = defineProps<{
  isEditorFullscreen?: boolean,
  options?: Array<{
    option?: string,
    isCorrect?: number,
    analysis?: string
  }>,
  score?: number
}>()

// 选择题选项数据结构
const multipleChoiceOptions = ref([
  {id: 1, content: '', isCorrect: 0, analysis: '', showAnalysis: false},
  {id: 2, content: '', isCorrect: 0, analysis: '', showAnalysis: false},
  {id: 3, content: '', isCorrect: 0, analysis: '', showAnalysis: false},
  {id: 4, content: '', isCorrect: 0, analysis: '', showAnalysis: false}
])
// 添加分值
const score = ref(0)

// 监听score属性变化
watch(() => props.score, (newScore) => {
  if (newScore !== undefined) {
    score.value = newScore;
  }
}, {immediate: true});

// 添加选项
const addOption = () => {
  const newId = multipleChoiceOptions.value.length > 0
      ? Math.max(...multipleChoiceOptions.value.map(option => option.id)) + 1
      : 1
  multipleChoiceOptions.value.push({id: newId, content: '', isCorrect: 0, analysis: '', showAnalysis: false})
}

// 删除选项
const removeOption = (id: number) => {
  const index = multipleChoiceOptions.value.findIndex(option => option.id === id)
  if (index !== -1) {
    multipleChoiceOptions.value.splice(index, 1)
  }
}

// 切换解析区域显示
const toggleAnalysis = (option: any) => {
  option.showAnalysis = !option.showAnalysis
}

// 获取去除id的选项数据，用于提交
const getOptions = () => {
  return multipleChoiceOptions.value.map(option => ({
    option: option.content,
    isCorrect: option.isCorrect,
    analysis: option.analysis
  }))
}

// 获取分值
const getScore = () => {
  return score.value
}

// 初始化选项数据
const initOptionsData = () => {
  if (props.options && props.options.length > 0) {
    // 清空默认选项
    multipleChoiceOptions.value = [];
    // 将传入的选项数据转换为内部数据结构
    props.options.forEach((option, index) => {
      multipleChoiceOptions.value.push({
        id: index + 1,
        content: option.option || '',
        isCorrect: option.isCorrect ?? 0,
        analysis: option.analysis || '',
        showAnalysis: !!option.analysis
      });
    });
  } else {
    // 如果没有传入选项，使用默认选项
    multipleChoiceOptions.value = [
      {id: 1, content: '', isCorrect: 0, analysis: '', showAnalysis: false},
      {id: 2, content: '', isCorrect: 0, analysis: '', showAnalysis: false},
      {id: 3, content: '', isCorrect: 0, analysis: '', showAnalysis: false},
      {id: 4, content: '', isCorrect: 0, analysis: '', showAnalysis: false}
    ];
  }
}

// 监听options属性变化
watch(() => props.options, () => {
  initOptionsData();
}, {immediate: true, deep: true});

// 暴露给父组件的方法
defineExpose({
  getOptions,
  getScore
})
</script>

<template>
  <el-form-item
      prop="multipleChoice"
      :class="{ 'hide-when-fullscreen': isEditorFullscreen }"
  >
    <div class="question-container">
      <div class="question-header">
        <div class="header-left">
          <h3 class="section-title">选择题选项<span class="required-asterisk">*</span></h3>
        </div>
        <div>
          <el-button type="primary" :icon="Plus" @click="addOption">
            添加选项
          </el-button>
        </div>
      </div>

      <div class="options-wrapper">
        <!-- 添加分值区域 -->
        <div class="score-section">
          <div class="section-header">
            <span class="section-label">分值</span>
            <el-tag size="small" effect="plain" type="info">SCORE</el-tag>
          </div>
          <div class="score-input-wrapper">
            <el-input-number
                v-model="score"
                :min="0"
                :max="100"
                controls-position="right"
                placeholder="分值"
                class="score-input"
            />
          </div>
        </div>

        <span class="section-label">选项</span>
        <div v-for="option in multipleChoiceOptions" :key="option.id" class="option-item">
          <div class="option-content">
            <el-checkbox
                v-model="option.isCorrect"
                class="option-checkbox"
                :true-value="1"
                :false-value="0"
            >
              正确答案
            </el-checkbox>
            <el-input
                v-model="option.content"
                placeholder="请输入选项内容"
                class="option-input"
            />
            <div class="option-actions">
              <el-button
                  type="primary"
                  :icon="Edit"
                  text
                  @click="toggleAnalysis(option)"
                  class="analysis-btn"
              >
                {{ option.showAnalysis ? '隐藏解析' : '添加解析' }}
              </el-button>
              <el-tag v-if="option.analysis && !option.showAnalysis" size="small" type="success" effect="light">
                <el-icon>
                  <Document/>
                </el-icon>
                <span>已添加解析</span>
              </el-tag>
              <el-button
                  v-if="multipleChoiceOptions.length > 2"
                  type="danger"
                  :icon="Delete"
                  circle
                  plain
                  size="small"
                  @click="removeOption(option.id)"
              />
            </div>
          </div>
          <div v-if="option.showAnalysis" class="option-analysis">
            <el-input
                v-model="option.analysis"
                type="textarea"
                :rows="2"
                placeholder="请输入选项解析（可选）"
                class="analysis-input"
            />
          </div>
        </div>
      </div>
    </div>
  </el-form-item>
</template>

<style scoped>
.question-container {
  width: 100%;
  background: var(--el-bg-color);
  border-radius: 8px;
  padding: 20px;
  border: 1px solid var(--el-border-color);
  margin-bottom: 16px;
}

.question-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.section-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  position: relative;
  padding-bottom: 8px;
}

.section-title::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: 0;
  width: 24px;
  height: 2px;
  background-color: var(--el-color-primary);
  border-radius: 1px;
}

.required-asterisk {
  color: var(--el-color-danger);
  margin-left: 4px;
}

.options-wrapper {
  position: relative;
}

/* 选择题样式 */
.option-item {
  margin-bottom: 16px;
  background-color: var(--el-fill-color-light);
  border-radius: 6px;
  padding: 12px;
  transition: all 0.3s;
}

.option-item:hover {
  background-color: var(--el-fill-color);
}

.option-content {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.option-checkbox {
  flex: 0 0 auto;
}

.option-input {
  flex: 1;
}

.option-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.analysis-btn {
  font-size: 12px;
}

.option-analysis {
  margin-top: 8px;
  padding-left: 24px;
  animation: fadeIn 0.3s ease-in-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.analysis-input {
  width: 100%;
}

.hide-when-fullscreen {
  display: none !important;
}

/* 添加分值样式 */
.score-section {
  width: 200px;
  margin-bottom: 20px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.section-label {
  font-size: 14px;
  font-weight: 500;
  color: var(--el-text-color-primary);
  position: relative;
  padding-left: 10px;
}

.section-label::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 14px;
  background-color: var(--el-color-primary);
  border-radius: 1.5px;
}

.score-input-wrapper {
  display: flex;
  align-items: flex-start;
}

:deep(.score-input) {
  width: 100%;
}

:deep(.score-input .el-input-number__decrease),
:deep(.score-input .el-input-number__increase) {
  border-radius: 0;
}

:deep(.score-input .el-input__wrapper) {
  padding-right: 30px;
}

@media screen and (max-width: 768px) {
  .option-content {
    flex-direction: column;
    align-items: flex-start;
  }

  .option-checkbox {
    margin-bottom: 8px;
  }

  .option-input {
    width: 100%;
    margin-bottom: 8px;
  }

  .option-actions {
    width: 100%;
    justify-content: space-between;
  }

  .option-analysis {
    padding-left: 0;
  }
}
</style> 